import json
import os
import utils
import datetime
import collect
from openpyxl.utils import get_column_letter

# 设置单元格宽度
def set_columns_width(sheet, columns_width):
    for index, width in enumerate(columns_width):
        sheet.column_dimensions[get_column_letter(index + 1)].width = width

def test():
    f = open("link_reader_number.json", 'r', encoding='utf-8')
    dest_filename = '{0} 链接阅读数表格.xlsx'.format(datetime.date.today())
    workbook, sheet = utils.make_workbook()
    set_columns_width(sheet, [20, 20, 50, 18])
    sheet.append(['账号', 'gh_id', '点链接', '阅读'])
    for line in f.readlines():
        line = json.loads(line)
        dic = [line['nickname'], line['gh_id'], line['link_url'], line['reader_number']]
        sheet.append(dic)
    workbook.save(filename=dest_filename)
    f.close()
    os.remove('link_reader_number.json')

if __name__ == '__main__':
    collect.test_collect()
    test()

